from random import randint
def get_list(num = 5):
    li = [randint(0, 100) for i in range(num)]
    return li
def cal_min_diff(l_max, l_min): 
    while True:
        l_max.sort(reverse=True)
        l_min.sort()
        sum1 = sum(l_max)
        sum2 = sum(l_min)
        diff = sum1 - sum2
        if diff < 0:
            temp = l_min
            l_min = l_max
            l_max = temp 

        diff = abs(diff)
        if diff <= 1:
            break
        b_find = False
        for j in range(len(l_min)):
            for i in range(len(l_max)):
                val = l_max[i] - l_min[j]
                if val < diff and val > 0:
                    b_find = True
                    break
            if b_find == True:
                break
        if b_find == False:
            break

        temp = l_max[i]
        l_max[i] = l_min[j]
        l_min[j] = temp
    l_max.sort()
    l_min.sort()
    return (l_max, l_min, diff)

if __name__ == '__main__':
    for i in range(100):
        l1 = get_list(10)
        l2 = get_list(10)
        l_max, l_min, min_diff = cal_min_diff(l1,l2)
        if min_diff > 1:
            print 'origin data'
            print l1, sum(l1)
            print l2, sum(l2)
            print '='*20
            print 'final data'
            print l_max,sum(l_max)
            print l_min, sum(l_min)
            print 'the minimal difference is ',min_diff 

